<template>
  <div class="h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1 form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="姓名/助记码/手机号" />
        </a-form-item>

        <a-form-item name="stdate" label="购买日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>

        <a-form-item name="mc" label=" ">
          <a-input v-model:value="formState.mc" class="w-64" placeholder="理疗项目/单据" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-start mb-2">
        <BaseExport
          type="理疗"
          name="购买记录查询"
          code="00001145"
          :search-params="{ ...formState, startTime: formState.dateRange?.[0] + ' 00:00:00', endTime: formState.dateRange?.[1] + ' 23:59:59' }"
          >导出</BaseExport
        >
      </div>
      <BaseTable :loading="loading" column-code="00001145" :data-source="tableData" :pagination="pagination" :scroll="{ x: 1000 }" fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'czsj'">
            {{ dayjs(record.czsj).format('YYYY-MM-DD') }}
          </template>
          <template v-if="column.dataIndex === 'dj'">
            {{ parseFloat(record.dj).toFixed(3) }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag :color="record.status !== 0 ? 'red' : 'green'">{{ record.status !== 0 ? '停用' : '启用' }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickPopup(record, 0)">服务项目</a-button>

            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onClickPopup(record, 1)">消费记录</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Popup v-model:open="editOpen" :title="openTitle" :type="popupType" :row-data="rowData" @success="onSuceess" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import Popup from './../components/Popup'
// import { post } from '@/utils/request'
// import { message } from 'ant-design-vue'

const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const editOpen = ref(false)
const rowData = ref(null)
const formRef = ref(null)
const popupType = ref('')
const openTitle = ref('')
const { loading, tableData, getData, pagination } = useTable({
  url: '/course/statisticalQuery/getBuyHistoryList',
  immediate: true,
  formatParams: () => {
    return {
      startTime: formState.value.dateRange?.[0] ? formState.value.dateRange?.[0] + ' 00:00:00' : null,
      endTime: formState.value.dateRange?.[1] ? formState.value.dateRange?.[1] + ' 23:59:59' : null,
      ...formState.value
    }
  }
})
const onSuceess = () => {
  getData()
}

const onClickPopup = (record, type) => {
  editOpen.value = true
  openTitle.value = type === 0 ? '服务项目查询' : '消费记录查询'
  rowData.value = record
  popupType.value = type === 0 ? '服务项目' : '消费记录'
}
</script>

<style lang="less" scoped></style>
